package com.gomore.opple;

import android.os.Handler;
import android.os.Looper;
import android.support.multidex.MultiDexApplication;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Toast;

import com.gomore.opple.data.DaggerDataRepositoryComponent;
import com.gomore.opple.data.DataRepositoryComponent;
import com.gomore.opple.module.DefineCockroach;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;

/**
 * Created by asus on 2017/5/9.
 */
public class OppleApplication extends MultiDexApplication {

    private DataRepositoryComponent mRepositoryComponent;
    private static OppleApplication mInstance;

    private static DisplayMetrics displayMetrics;

    public static OppleApplication getInstance() {
        return mInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        mRepositoryComponent = DaggerDataRepositoryComponent.builder().applicationModule(new ApplicationModule(getApplicationContext())).build();
        displayMetrics = getResources().getDisplayMetrics();
        Config.DEBUG = true;
        UMShareAPI.get(this);
        setShare();
//        initCrash();
    }

    public DataRepositoryComponent getRepositoryComponent() {
        return mRepositoryComponent;
    }

    public static DisplayMetrics getDisplayMetrics() {
        return displayMetrics;
    }

    private void setShare() {
        PlatformConfig.setWeixin("wxbf9560f71bca3ca1", "903025a49b21ce61eb892d52736b1164");
        PlatformConfig.setSinaWeibo("1450011937", "a79510fd9610a3aa2c2e43a16b1c2596", "https://sns.whalecloud.com/sina2/callback");
    }

    /**
     * 初始化捕获异常
     */
    private void initCrash() {
        DefineCockroach.install(new DefineCockroach.ExceptionHandler() {
            // handlerException内部建议手动try{  你的异常处理逻辑  }catch(Throwable e){ },
            // 以防handlerException内部再次抛出异常，导致循环调用handlerException
            @Override
            public void handlerException(final Thread thread, final Throwable throwable) {
                //开发时使用Cockroach可能不容易发现bug，所以建议开发阶段在handlerException中用Toast谈个提示框，
                //由于handlerException可能运行在非ui线程中，Toast又需要在主线程，所以new了一个new Handler(Looper.getMainLooper())，
                //所以千万不要在下面的run方法中执行耗时操作，因为run已经运行在了ui线程中。
                //new Handler(Looper.getMainLooper())只是为了能弹出个toast，并无其他用途
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            //建议使用下面方式在控制台打印异常，这样就可以在Error级别看到红色log
                            Log.e("AndroidRuntime", "--->CockroachException:" + thread + "<---", throwable);
                            Toast.makeText(mInstance, "Exception Happend\n" + thread + "\n" + throwable.toString(), Toast.LENGTH_SHORT).show();
                        } catch (Throwable e) {

                        }
                    }
                });
            }
        });
    }
}
